package com.engine.modules.vector.controller;

import com.engine.common.baseEntity.jsonResponse.JsonResponseEntity;
import com.engine.common.constant.CommonConstant;
import com.engine.common.exception.InvalidTokenRuntimeException;
import com.engine.common.util.ListMapAndJson;
import com.engine.common.util.TokenVerifyUtil.TokenVerifyUtil;
import com.engine.modules.vector.mapper.VectorSourceMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "4-矢量数据高级接口")
@ApiSort(value = 4)
@CrossOrigin
@RequestMapping("/Vector/Advance")
@Slf4j
public class VectorAdvanceController {

    @Autowired
    VectorSourceMapper vectorSourceMapper;

    @ApiOperation(value = "Geometry统计查询", notes = "通过Geometry（此Geometry是geojson中的数据格式），获取数据，返回带统计信息的geojson")
    @PostMapping(value = "/countGeojsonByGeometry/{tableName}/{token}")
    @ApiOperationSupport(order = 1)
    public String countGeojsonByGeometry(@PathVariable("tableName") String tableName,
                                         @RequestParam(value = "countColumns") String countColumns,
                                         @RequestBody String geometry,
                                         @PathVariable("token") String token,
                                         HttpServletResponse response) {
        try {
            String access = TokenVerifyUtil.tokenVerify(token);
            return vectorSourceMapper.countGeojsonByGeometry(geometry, countColumns, tableName, access);
        } catch (InvalidTokenRuntimeException e)
        {
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_BK_ERROR, "token错误，e：" + e.getMessage(),
                    response);
            return jsonResponseEntity.getJsonString();
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_BK_ERROR, "查询出错，e：" + e.getMessage(), response);
            return jsonResponseEntity.getJsonString();
        }
    }

    @ApiOperation(value = "Geometry邻近查询", notes = "通过Geometry（此Geometry是geojson中的数据格式），获取距此Geometry最近的n个矢量的数据，返回geojson")
    @PostMapping(value = "/selectMinNeighberGeojsonByGeometry/{token}")
    @ApiOperationSupport(order = 2)
    public String selectMinNeighberGeojsonByGeometry(@PathVariable("tableName") String tableName,
                                                     @RequestParam(value = "getCount") Integer getCount,
                                                     @RequestBody String geometry,
                                                     @PathVariable("token") String token,
                                                     HttpServletResponse response) {
        try {
            String access = TokenVerifyUtil.tokenVerify(token);
            List<Map<String, Object>> rsList = vectorSourceMapper.selectMinNeighberGeojsonByGeometry(geometry, getCount, tableName, access);
            return ListMapAndJson.ListMap2Geojson(rsList);
        } catch (InvalidTokenRuntimeException e)
        {
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_BK_ERROR, "token错误，e：" + e.getMessage(),
                    response);
            return jsonResponseEntity.getJsonString();
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_BK_ERROR, "查询出错，e：" + e.getMessage(), response);
            return jsonResponseEntity.getJsonString();
        }
    }

}
